package org.kinglone.back.friendlinks.service;

import org.kinglone.back.friendlinks.model.Friendlinks;
import org.kinglone.back.friendlinks.service.FriendlinksService;
import org.kinglone.common.dao.impl.BaseDaoImpl;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.kinglone.common.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service("friendlinksService")
@Transactional(readOnly=false)
public class FriendlinksServiceImpl extends BaseDaoImpl<Friendlinks> implements FriendlinksService {

    @Override
    public PageResults<Friendlinks> findLinksList(PageQuery pageQuery, String nickname, String phone){
        StringBuffer hql = new StringBuffer("from Friendlinks f where 1=1 ");
        StringBuffer whereHql = new StringBuffer();
        Map<String, Object> queryParamMap = new HashMap<String, Object>();
        // 名称
        if (!StringUtil.isNullOrEmpty(nickname)) {
            whereHql.append(" and nickname like :name");
            queryParamMap.put("name", '%'+ nickname +'%');
        }
        //手机
        if (!StringUtil.isNullOrEmpty(phone)) {
            whereHql.append(" and phone like :phone");
            queryParamMap.put("phone",'%'+phone +'%');
        }
        StringBuffer countsql = new StringBuffer(" select count(0) from friendlinks where 1=1 ");
        PageResults<Friendlinks> pageResults = super.findPageByHqlAndCountSql(hql.append(whereHql).toString(),countsql.append(whereHql).toString(),pageQuery, queryParamMap, queryParamMap);
        return pageResults;
    }
}
